//
//  GroupCreater.swift
//  eemall
//
//  Created by zhuxietong on 2018/12/25.
//  Copyright © 2018 tianrun. All rights reserved.
//


import UIKit
import soapp
import AsyncDisplayKit
import JoLoading
import Eelay


open class GroupAlert: EeAlertView,ASTableVConfig,LoadingPresenter {
    public let header = UIView()
    public let canleBt = UIButton()
    
    public var loadingPresenterInset: UIEdgeInsets{
        get{
            return .zero
        }
    }
    
    open func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        
    }
    
    
    public var LoadingPresenterBg: UIView{
        return contentView
    }
    
    public var whiteView = UIView()
    public var tableNode:ASTableNode = ASTableNode(style: .plain)
    public var tableView:UITableView{
        get{
            return tableNode.view
        }
    }
    
    var selector = [
        "<default>":JoCellNode.self
    ]
    
    open var sections:TP.section{
        get{
            let sec:TP.section = [
                [
                    ["cell":"one"],
                    
                ]
            ]
            return sec
        }
    }
    
    open override func willLayoutRules() {
        whiteView.backgroundColor = ._cellBackground
        jo_contentView.eelay = [
            [whiteView,[ee.T.L.B.R,[10,0,-10,0]]],
            [canleBt,[whiteView,ee.R.T,ee.X.Y]],
        ]
        whiteView.bsui.background(hex: "#fff").set(radius: 3, width: nil, color: nil)
        tableView.separatorStyle = .none
        tableView.hidenMoreLine()
        configTables(nodes: tableNode)
        canleBt.setImage(UIImage(named: "tg-guangbi"), for: .normal)
        canleBt.addTarget(self, action: #selector(hiddenAlert), for: .touchUpInside)
    }
    
    open var contentHeight:CGFloat{
        get{
            var count = sections[0].count
            if count > 5{
                count = 5
            }
            return CGFloat(count * 50)
        }
    }
    open override var intrinsicContentSize: CGSize{
        return [(Swidth-AppTheme.edge * 4.0),(contentHeight + 60 + 20)]
    }
    
   
    @objc func hiddenAlert(){
        dismiss()
    }
    
}



open class GroupCreater: GroupAlert{
    
    static func create(goods:NSMutableDictionary, at:UIViewController?,confirm:@escaping (_:NSMutableDictionary?)->Void){

        if goods["is_ladder",false.tag] == true.tag{
            let alert = GroupCreater()
            alert.selectedGroup = confirm
            alert.mo.controller = at
            alert.mo.model = goods
            alert.show()
        }
        else{
            confirm(nil)
        }
    }
    
    var selectedGroup:(_:NSMutableDictionary?)->Void = {_ in }
    
    let deleteBt = UIButton()
    
    open override func addLayoutRules() {
        
        
        whiteView.eelay = [
            [header,[ee.T.L.R],"50"],
            [deleteBt,[header,ee.R.Y,[-11,0]],"20",20],
            [tableView,[ee.L.R.B,[0,0,-10]],[header,ee.B,ee.T]],
        ]
        
        let titleL = UILabel()
        header.eelay = [
            [titleL,[ee.X.Y]],
        ]
        titleL.attributedText = "请选择组团人数".loc.ui.bfont13.color(color: ._foreground).astring

        header.backgroundColor = ._cellBackground
        tableView.backgroundColor = ._cellBackground
        whiteView.backgroundColor = ._cellBackground
        selector = [
            "<default>":ItemCell.self
        ]
        
        deleteBt.ui.nimage(UIImage(named: "delete-tag-0")).padding(insets: [5])
        deleteBt.addTarget(self, action: #selector(hiddenAlert), for: .touchUpInside)
        mo_model_reload()
    }

    open override var contentHeight:CGFloat{
        get{
            var count = DataNode.path(node: "ladder", mo.model, value: NSMutableArray()).count
            if count > 5{
                count = 5
            }
            return CGFloat(count * 50)
        }
    }
    
    override open var sections:TP.section{
        get{
            let sec:TP.section = [
                [
                    ["cell":"one"],
                ]
            ]
            return sec
        }
    }
    
    open override func mo_model_reload() {
        tableNode.load(sections: sections, selector: selector)
        tableNode.model <-- ("section0",DataNode.path(node: "ladder", mo.model, value: NSMutableArray()),.arrayRefresh)
        tableNode.reloadData()
    }
    
    
    public func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
        guard let obj = tableNode.cellData(at: indexPath) else {
            return
        }
        
        selectedGroup(obj)
        dismiss()
    }
}

extension GroupCreater{
    
   
    class ItemCell: JoCellNode {
        let priceL = ASTextNode2()
        let numL = ASTextNode2()
        let nameL = ASTextNode2()
        
        let createBt = ASButtonNode()
        
        override func addLayoutRules() {
            view.backgroundColor = ._cellBackground
        }
        
        override func mo_model_reload() {
            let name = "[XX]人团购".replaceVars(values: mo.model["ladder_num","0"])
            nameL.attributedText = name.ui.font14.color(color: ._foreground).astring
            let price = mo.model[float:"ladder_price",0]
            priceL.attributedText = "¥ ".ui.font11.color(hex: AppTheme.main_hex).astring + "\(price.price_display)".ui.font13.color(hex: AppTheme.main_hex).astring

//            numL.attributedText = "1天20小时".ui.font12.cl_444.astring
            
            createBt.ui.font(size: 11).ncolor(hex: "#fff").padding(insets: [7,12]).ntitle(title: "发起拼单".loc).buttonColor(hex: AppTheme.main_hex).set(radius: 3, width: nil, color: nil)
        }
   
        override func specGetter() -> _Spec {
            let spec = _Spec.stack(["h|30",[10,16],&-.center], childs: [
                .stack(["h|10","30",_justfy(.space_between),_grow(1),_shrink(1),&-.center], childs: [
                    nameL.Spec,
                    priceL.Spec,
                    ]),
                createBt.Spec
                ])
            
            return spec
        }
    }
}
